<?php
namespace app\team\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

/**
 * 角色模块
 */
class Role extends Common{
    /**
     * 角色页面展示
     */
    public function role(){
        //检查权限
        $authCheck = parent::checkAuth(request()->controller() .'/'. request()->action());
        if ($authCheck == '2'){
            die('暂无权限');
        }
        return view();
    }
    /**
     * 角色列表数据
     */
    public function roleList(){
        $data = Db::name('team_auth_group')->where('team_id',Session::get('team_id'))->select();
        $data = array('code'=>0,'msg'=>'','data'=>$data);
        echo json_encode($data);
    }
    /**
     * 添加角色
     */
    public function add(){
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $result = Db::name('team_auth_group')->insert($post['data']);
            if ($result) {
                echo "1";
            }else{
                echo "2";
            }
        }
    }

    /**
     * 角色修改
     */
    public function editRole(){
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $result = Db::name('team_auth_group') -> update($post['data']);
            if ($result) {
                echo '1';
            }else{
                echo "2";
            }
        }
    }


    /**
     * 角色删除
     */
    public function del(){
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();

            //该角色存在管理员禁止删除
            $admin = Db::name('team_auth_group_access')->where("group_id",$post['id'])->find();
            if(!$admin){
                $result = Db::name('team_auth_group')->delete($post['id']);
                if ($result){
                    echo '1';
                }else{
                    echo '系统错误，删除失败';
                }
            }else{
                echo '该角色下存在管理员，禁止删除';
            }
        }
    }

    /**
     * 权限分配检索
     */
    public function auth(){
        $post = Request::instance()->post();
        $id = $post['id'];

        $group = Db::name('team_auth_group')->field('rules')->where("id",$id)->find();
        $arr = explode(',',$group['rules']);

        $data = Db::name('team_auth_rule')->field('title as name,pid,id as value')->where('pid',0)->select();
        foreach($data as $key => $value){
            if(in_array($value['value'],$arr)){
                $data[$key]['checked'] = 1;
            }else{
                $data[$key]['checked'] = 0;
            }
            $childData = Db::name('team_auth_rule')->field('title as name,pid,id as value')->where("pid" ,$value['value'])->select();
            foreach($childData as $k => $val){
                if(in_array($val['value'],$arr)){
                    $childData[$k]['checked'] = 1;
                }else{
                    $childData[$k]['checked'] = 0;
                }
            }
            $data[$key]['list'] = $childData;
        }
        return json(array('code'=>0,'msg'=>'获取成功','data'=>array('trees'=>$data)));

    }

    /**
     * 设置权限分配
     */
    public function authSet(){
        $post = Request::instance()->post();
        $data['id'] = $post['data']['id'];
        unset($post['data']['id']);
        foreach($post['data'] as $value){
            $str[] = $value;
        }
        $str = implode(",", $str);
        $data['rules'] = $str;
        $res = Db::name('team_auth_group')->update($data);
        if ($res){
            echo '1';
        }else{
            echo '权限设置失败';
        }
    }

    /**
     * 修改等级
     */
    public function editLevel(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $result = Db::name('team_auth_group') ->update($post);
            if ($result) {
                echo '1';
            }else{
                echo "2";
            }
        }
    }
}
